﻿using System.Globalization;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;

namespace WindowsFormsApplication1.Common
{
    public partial class GeoModel
    {
        [JsonProperty("status")]
        public string Status { get; set; }

        [JsonProperty("info")]
        public string Info { get; set; }

        [JsonProperty("infocode")]
        public string Infocode { get; set; }

        [JsonProperty("count")]
        public string Count { get; set; }

        [JsonProperty("geocodes")]
        public Geocode[] Geocodes { get; set; }
    }

    public partial class Geocode
    {
        [JsonProperty("formatted_address")]
        public object FormattedAddress { get; set; }

        [JsonProperty("province")]
        public object Province { get; set; }

        [JsonProperty("citycode")]
        public object Citycode { get; set; }

        [JsonProperty("city")]
        public object City { get; set; }

        [JsonProperty("district")]
        public object District { get; set; }

        //[JsonProperty("township")]
        //public object[] Township { get; set; }

        //[JsonProperty("neighborhood")]
        //public Building Neighborhood { get; set; }

        //[JsonProperty("building")]
        //public Building Building { get; set; }

        [JsonProperty("adcode")]
        public object Adcode { get; set; }

        //[JsonProperty("street")]
        //public object Street { get; set; }

        [JsonProperty("number")]
        public object Number { get; set; }

        [JsonProperty("location")]
        public object Location { get; set; }

        //[JsonProperty("level")]
        //public string Level { get; set; }
    }

    public partial class Building
    {
        [JsonProperty("name")]
        public object[] Name { get; set; }

        [JsonProperty("type")]
        public object[] Type { get; set; }
    }

    public partial class GeoModel
    {
        public static GeoModel FromJson(string json) => JsonConvert.DeserializeObject<GeoModel>(json, Converter.Settings);
    }

    public static class Serialize
    {
        public static string ToJson(this GeoModel self) => JsonConvert.SerializeObject(self, Converter.Settings);
    }

    internal class Converter
    {
        public static readonly JsonSerializerSettings Settings = new JsonSerializerSettings
        {
            MetadataPropertyHandling = MetadataPropertyHandling.Ignore,
            DateParseHandling = DateParseHandling.None,
            Converters = {
                new IsoDateTimeConverter { DateTimeStyles = DateTimeStyles.AssumeUniversal }
            },
        };
    }
}
